#!/bin/bash
#========================================================================
# Author: YSLin
# Email: 
# File Name: loop.sh
# Description: 
#  0.break and continue
#
#  0.infinite loop
#	while :
#	do
#		list
#	done

#  1.while loop
#   while command
# 	do
#		list
# 	done
#  2.short while loop
#   while command ; do list ; done
#
#  3.Nesting while Loops
# 	while command1 ; # this is loop1, the outer loop
# 	do
#		list1
#		while command2 ; # this is loop2, the inner loop
#		do
#			list2
#		done
#		list3
#	done
#  4.until 
# 	until command
# 	do
#		list
# 	done
#  5.short until
#   until command ; do list ; done
#
#  6.for loop
#	for name in word1 word2 ... wordN
#	do
#		list
#	done

#  7.short for loop
#   for name in word1 word2 ... wordN ; do list ; done

#  8.select loop
#	select name in word1 word2 ... wordN
#	do
#		list
#	done

#  9.unset PS3 來控制select顯示的提示字元

# Edit History: 
#   2010-12-21    File created.
#========================================================================

#0.break
function readCMD(){
while :
do
	read CMD
	case $CMD in
		[qQ]|[qQ][uU][iI][tT]) 
			break ;;
		*) process $CMD ;;
	esac
done
}

function backup(){
for i in 1 2 3 4 5
do
	mkdir -p /mnt/backup/docs/ch0${i}
	if [ $? -eq 0 ] ; then
		for j in doc c h m pl sh
		do
			cp $HOME/docs/ch0${i}/*.${j} /mnt/backup/docs/ch0${i}
			#break 2表示跳出兩個迴圈
			if [ $? -ne 0 ] ; then break 2 ; fi
		done
	else
		echo "Could not make backup directory."
	fi
done
}

#0.continue
for FILE in $FILES ;
do
	if [ ! -f "$FILE" ] ; then
		echo "ERROR: $FILE is not a file."
		continue
	fi
	# process the file
done
#計算1到10
x=0
while [ $x -lt 10 ]
do
	echo $x
	x=`echo "$x + 1" | bc`
done
#計算1到10
x=0
while [ $x -lt 10 ]
do
    echo $x
    x=`expr $x + 1`
done


x=0
while [ "$x" -lt 3 ] ; # this is loop1
do
	y="$x"
	while [ "$y" -ge 0 ] ; # this is loop2
	do
		echo -n "$y "
		y=`echo "$y - 1" | bc`
	done
	echo
	x=`echo "$x + 1" | bc`
done

function readDir(){
RESPONSE=
while [ -z "$RESPONSE" ] ;
do
	echo "Enter the name of a directory where your files are located:\c "
	read RESPONSE
	if [ ! -d "$RESPONSE" ] ; then
		echo "ERROR: Please enter a directory pathname."
		RESPONSE=
	fi
done
}

x=1;
until [ $x -ge 10 ]
do
	echo $x
	x=`echo "$x + 1" | bc`
done

#==============================================================================
# 6.for loop
#==============================================================================
array=(
"hello world"
"array"
)
#印出每個array的成員
for ((i=0; i<${#array[@]}; i++)); do    #請注意 ((   )) 雙層括號
	printf "%s \n" "${array[$i]}" 
done

#印出0~9
for i in 0 1 2 3 4 5 6 7 8 9
do
	echo -n $i
done

#用`find . -name ".bash*"`會找到包含空白的檔案名稱,造成$FILE沒辦法在一個變數中顯示
function cpfiles(){
for FILE in $HOME/.bash*
do
	cp $FILE ${HOME}/public_html
	chmod a+r ${HOME}/public_html/${FILE}
done
}

function findFile(){
for file in `find . -type f`
do
	echo $file;
done
}


exit
#Changing the Prompt
#You can change the prompt displayed by the select loop by altering the variable PS3. 
#If PS3 is not set, the default prompt, #?, is displayed. 
#Otherwise the value of PS3 is used as the prompt to display. 
PS3="Please make a selection => " ; export PS3
#unset PS3

#==============================================================================
# 8.select loop
#==============================================================================
#easy way to create a numbered menu from which users can select options
select COMPONENT in comp1 comp2 comp3 all none
do
	case $COMPONENT in
		comp1|comp2|comp3) 
			CompConf $COMPONENT 
			;;
		all) 
			CompConf comp1
			CompConf comp2
			CompConf comp3
			;;
		none) 
			break ;;
		*) 
			echo "ERROR: Invalid selection, $REPLY." ;;
	esac
done



